<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      :columns="columns"
      :request-api="getTableList"
      :init-param="initParam"
      :data-callback="dataCallback"
      @drag-sort="sortTable"
    >
      <template #tableHeader>
        <el-button type="primary" :icon="CirclePlus" @click="handleCreateEvent">新增权限</el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-space direction="vertical">
          <el-button type="primary" link :icon="Delete" @click="handleDeletePackage(scope)">删除</el-button>
        </el-space>
      </template>
    </ProTable>
    <PermissionModal v-model="dialogVisible" @success="handleSuccess" />
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { ref, reactive } from "vue";
import { User } from "@/api/interface";
import { ElMessage, ElMessageBox } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete } from "@element-plus/icons-vue";
import PermissionModal from "./PermissionModal.vue";
import { getShopAuthListApi, deleteShopAuthApi } from "@/api/modules/permission";

// ProTable 实例
const dialogVisible = ref(false);
const proTable = ref<ProTableInstance>();
// 如果表格需要初始化请求参数，直接定义传给 ProTable (之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({
  pageNum: 1,
  pageSize: 10
});

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total 这些字段，可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
  return {
    list: data.list,
    total: data.total
  };
};

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = async () => {
  return getShopAuthListApi({
    page: initParam.pageNum,
    limit: initParam.pageSize
  });
};
const handleCreateEvent = () => {
  dialogVisible.value = true;
};
const handleDeletePackage = (scope: any) => {
  ElMessageBox.confirm("请确认删除", "删除提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    beforeClose(action, instance, done) {
      if (action === "confirm") {
        done();
        return;
      }
      done();
    }
  })
    .then(() => {
      deleteShopAuthApi({
        auth_rule_id: scope.row.id
      }).then(res => {
        if (res.code === 1) {
          ElMessage.success("删除成功");
          handleSuccess();
        }
      });
    })
    .catch(() => {
      ElMessage.info("已取消删除");
    });
};
// 表格配置项
const columns = reactive<ColumnProps<User.ResUserList>[]>([
  { prop: "id", label: "ID", width: 80 },
  {
    prop: "name",
    label: "权限名称"
  },
  {
    prop: "path",
    label: "权限标识",
    width: 200
  },
  {
    prop: "create_time",
    label: "创建时间",
    width: 180
  },
  { prop: "operation", label: "操作", fixed: "left", width: 100 }
]);

// 表格拖拽排序
const sortTable = ({ newIndex, oldIndex }: { newIndex?: number; oldIndex?: number }) => {
  console.log(newIndex, oldIndex);
  console.log(proTable.value?.tableData);
  ElMessage.success("修改列表排序成功");
};
const handleSuccess = () => {
  proTable.value?.getTableList();
};
</script>

<style scoped>
.order-container {
  padding: 12px;
  margin-bottom: 20px;
  border-radius: 4px;
}
.positive-order {
  background-color: #f0f9eb;
  border: 1px solid #e1f3d8;
}
.negative-order {
  background-color: #fef0f0;
  border: 1px solid #fde2e2;
}
.order-title {
  margin-bottom: 10px;
  font-weight: bold;
  color: #333333;
}
.goods-item {
  padding: 8px 0;
  border-bottom: 1px dashed #ebeef5;
}
.goods-item:last-child {
  border-bottom: none;
}
.goods-name {
  margin-bottom: 4px;
  color: #606266;
}
.goods-price {
  font-size: 13px;
  color: #909399;
}
</style>
